package xin.alum.aim.rabbitmq;

import xin.alum.aim.cluster.ClusterPusher;
import xin.alum.aim.util.JSONUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

class RabbitMQPusher extends ClusterPusher {

    private final RabbitTemplate rabbitTemplate;

    RabbitMQPusher(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    protected <T> boolean channel(String channel, T d) {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.getHeaders().put("desc", "信息描述..");
        messageProperties.getHeaders().put("type", "消息类型：" + d.getClass().getName());
        Message message = new Message(JSONUtils.toJSONByte(d), messageProperties);
        rabbitTemplate.convertAndSend(channel, message);
        return true;
    }
}
